// 命令接口定义
interface ICommand<T = any> {
  readonly moduleId: String
  readonly service: string
  readonly method: string
  readonly params: any[]
  execute(): Promise<T>
}

// 服务类型枚举
enum ServiceType {
  BizService = 'IBizService',
  StructureService = 'IStructureService',
  BQItemService = 'IBQItemService',
  LmmDetailService = 'ILmmDetailService',
}

// 抽象命令基类
abstract class BaseCommand<T> implements ICommand<T> {
  constructor (
    public readonly moduleId: string,
    public readonly service: string,
    public readonly method: string,
    public readonly params: any[] = [],
  ) {}

  async execute(): Promise<T> {
    try {
      const serviceInstance = ServiceRegistry.getInstance().resolve(this.service)
      const methodRef = (serviceInstance as any)[this.method]

      if (typeof methodRef !== 'function') {
        throw new Error(`Method ${this.method} not found on ${this.service}`)
      }

      return await methodRef.apply(serviceInstance, this.params)
    } catch (error) {
      // 统一的错误处理
      ErrorHandler.handle(error)
      throw error
    }
  }
}

// 具体命令实现示例
class SaveProjectCommand extends BaseCommand<void> {
  constructor(projectId: string) {
    super(ServiceType.BizService, 'saveProject', [projectId])
  }
}

class CreateFolderCommand extends BaseCommand<string> {
  constructor(parentId: string, folderName: string) {
    super(ServiceType.StructureService, 'createFolder', [parentId, folderName])
  }
}
